/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t__req_parse.c                                     */
/*  NOTE      =                                                    */
/*  DATE      = 2016/03/31 by jikw                                 */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include <cassandra/cassandra_core.h>
#include "frcomm.h"
#include "frmem.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "frsutl.h"
#include "frreport_comm.h"
#include "../../../src/module/cassandra/config.h"

#include "frbyte_buffer.h"
#include "apmtest_cunit.h"

typedef struct __st_cassandra_report_list {
    uint16_t u16_stream;
    Frtrans_info st_trans_info;
    Frreport_body_data *p_st_req_list;
    struct __st_cassandra_report_list *next;
} cassandra_report_list;

typedef struct __st_L7session_cassandra_private
{
    unsigned long ul_seq;                             /*!< 序列号，1次req和response时，使用同一个序列号 */
    L4_transaction st_l4_trans;                       /*!< 数据 */
    Frreport_body_data *p_st_body_data_req;
    Frreport_body_data *p_st_body_data_resp;
    cassandra_report_list *p_lst_report;              /* 未上报的请求 */
    Frlist_header *p_lst_comm;
    Frlist_header *p_lst_req;
    Frbyte_Packet *p_req_packet;                      /*!< 存请求bytebuff数据 */
    Frbyte_Packet *p_resp_packet;                     /*!< 存相应bytebuff数据 */
    uint16_t u16_req_stream;
    uint16_t u16_resp_stream;
    int i_sign;
}L7session_cassandra_private;

extern int cassandra_session_destroy(void *p_private_session);
extern int __req_parse(Frbyte_Packet *p_packet
                        , L7session_cassandra_private *p_st_private
                        , Frreport_body_data *p_st_body_data
                        , Conntrack *p_st_conn);

static int __suite_init(void)
{

	return 0;
}

static int __suite_cleanup(void)
{

	return 0;
}

static void case1(void)
{
    int i_ret = 0;
    Frbyte_Packet *p_packet = NULL;
    L7session_cassandra_private *p_st_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Conntrack *p_st_conn = NULL;

    i_ret = __req_parse(p_packet, p_st_private, p_st_body_data, p_st_conn);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);

    return;
}

static void case2(void)//05
{
    int i_ret = 0;
	char *p_data = NULL;
    Frbyte_Packet *p_packet = NULL;
    L7session_cassandra_private *p_st_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Conntrack *p_st_conn = NULL;

    p_data = (char *)"\x04\x00\x00\x37\x05\x00\x00\x00\x00";
    p_packet = bytebuff_creat();
    if(p_packet == NULL)
        goto ERROR;
    if(bytebuff_add(p_packet, p_data, 9) != FR_SUCC)
        goto ERROR;

    p_st_private = (L7session_cassandra_private *)frmalloc(sizeof(L7session_cassandra_private));
    if(p_st_private == NULL)
        goto ERROR;
    memset(p_st_private, 0x00, sizeof(L7session_cassandra_private));

    p_st_body_data = (Frreport_body_data *)frmalloc(sizeof(Frreport_body_data));
    if(p_st_body_data == NULL)
        goto ERROR;
    memset(p_st_body_data, 0x00, sizeof(Frreport_body_data));
    p_st_body_data->em_type = FRREPORT_TYPE_L7DATA;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    i_ret = __req_parse(p_packet, p_st_private, p_st_body_data, p_st_conn);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SET_REQ(APM_L7_ANALYZE_SUCC));

    cassandra_session_destroy(p_st_private);
    frreport_body_free(&p_st_body_data);
    frfree(p_st_conn);
    if(p_packet)
    {
        bytebuff_free(&p_packet);
        p_packet = NULL;
    }
    return;

ERROR:
    if(p_st_private)
        cassandra_session_destroy(p_st_private);
    if(p_st_body_data)
        frreport_body_free(&p_st_body_data);
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_packet)
    {
        bytebuff_free(&p_packet);
        p_packet = NULL;
    }
    return;
}

static void case3(void)//01
{
    int i_ret = 0;
	char *p_data = NULL;
    Frbyte_Packet *p_packet = NULL;
    L7session_cassandra_private *p_st_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Conntrack *p_st_conn = NULL;

    p_data = (char *)"\x04\x00\x00\x01\x01\x00\x00\x00\x16\x00\x01\x00\x0b\x43\x51\x4c\x5f\x56\x45\x52\x53\x49\x4f\x4e\x00\x05\x33\x2e\x33\x2e\x31";
    p_packet = bytebuff_creat();
    if(p_packet == NULL)
        goto ERROR;
    if(bytebuff_add(p_packet, p_data, 31) != FR_SUCC)
        goto ERROR;

    p_st_private = (L7session_cassandra_private *)frmalloc(sizeof(L7session_cassandra_private));
    if(p_st_private == NULL)
        goto ERROR;
    memset(p_st_private, 0x00, sizeof(L7session_cassandra_private));

    p_st_body_data = (Frreport_body_data *)frmalloc(sizeof(Frreport_body_data));
    if(p_st_body_data == NULL)
        goto ERROR;
    memset(p_st_body_data, 0x00, sizeof(Frreport_body_data));
    p_st_body_data->em_type = FRREPORT_TYPE_L7DATA;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    i_ret = __req_parse(p_packet, p_st_private, p_st_body_data, p_st_conn);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SET_REQ(APM_L7_ANALYZE_SUCC));

    cassandra_session_destroy(p_st_private);
    frreport_body_free(&p_st_body_data);
    frfree(p_st_conn);
    if(p_packet)
    {
        bytebuff_free(&p_packet);
        p_packet = NULL;
    }
    return;

ERROR:
    if(p_st_private)
        cassandra_session_destroy(p_st_private);
    if(p_st_body_data)
        frreport_body_free(&p_st_body_data);
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_packet)
    {
        bytebuff_free(&p_packet);
        p_packet = NULL;
    }
    return;
}

static void case4(void)//0b
{
    int i_ret = 0;
	char *p_data = NULL;
    Frbyte_Packet *p_packet = NULL;
    L7session_cassandra_private *p_st_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Conntrack *p_st_conn = NULL;

    p_data = (char *)"\x04\x00\x00\x02\x0b\x00\x00\x00\x31\x00\x03\x00\x0f\x54\x4f\x50\x4f\x4c\x4f\x47\x59\x5f\x43\x48\x41\x4e\x47\x45\x00\x0d\x53\x54\x41\x54\x55\x53\x5f\x43\x48\x41\x4e\x47\x45\x00\x0d\x53\x43\x48\x45\x4d\x41\x5f\x43\x48\x41\x4e\x47\x45";
    p_packet = bytebuff_creat();
    if(p_packet == NULL)
        goto ERROR;
    if(bytebuff_add(p_packet, p_data, 58) != FR_SUCC)
        goto ERROR;

    p_st_private = (L7session_cassandra_private *)frmalloc(sizeof(L7session_cassandra_private));
    if(p_st_private == NULL)
        goto ERROR;
    memset(p_st_private, 0x00, sizeof(L7session_cassandra_private));

    p_st_body_data = (Frreport_body_data *)frmalloc(sizeof(Frreport_body_data));
    if(p_st_body_data == NULL)
        goto ERROR;
    memset(p_st_body_data, 0x00, sizeof(Frreport_body_data));
    p_st_body_data->em_type = FRREPORT_TYPE_L7DATA;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    i_ret = __req_parse(p_packet, p_st_private, p_st_body_data, p_st_conn);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SET_REQ(APM_L7_ANALYZE_SUCC));

    cassandra_session_destroy(p_st_private);
    frreport_body_free(&p_st_body_data);
    frfree(p_st_conn);
    if(p_packet)
    {
        bytebuff_free(&p_packet);
        p_packet = NULL;
    }
    return;

ERROR:
    if(p_st_private)
        cassandra_session_destroy(p_st_private);
    if(p_st_body_data)
        frreport_body_free(&p_st_body_data);
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_packet)
    {
        bytebuff_free(&p_packet);
        p_packet = NULL;
    }
    return;
}

static void case5(void)//07
{
    int i_ret = 0;
	char *p_data = NULL;
    Frbyte_Packet *p_packet = NULL;
    L7session_cassandra_private *p_st_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Conntrack *p_st_conn = NULL;

    p_data = (char *)"\x04\x00\x00\x05\x07\x00\x00\x00\x2c\x00\x00\x00\x25\x53\x45\x4c\x45\x43\x54\x20\x2a\x20\x46\x52\x4f\x4d\x20\x73\x79\x73\x74\x65\x6d\x2e\x73\x63\x68\x65\x6d\x61\x5f\x6b\x65\x79\x73\x70\x61\x63\x65\x73\x00\x01\x00";
    p_packet = bytebuff_creat();
    if(p_packet == NULL)
        goto ERROR;
    if(bytebuff_add(p_packet, p_data, 53) != FR_SUCC)
        goto ERROR;

    p_st_private = (L7session_cassandra_private *)frmalloc(sizeof(L7session_cassandra_private));
    if(p_st_private == NULL)
        goto ERROR;
    memset(p_st_private, 0x00, sizeof(L7session_cassandra_private));

    p_st_body_data = (Frreport_body_data *)frmalloc(sizeof(Frreport_body_data));
    if(p_st_body_data == NULL)
        goto ERROR;
    memset(p_st_body_data, 0x00, sizeof(Frreport_body_data));
    p_st_body_data->em_type = FRREPORT_TYPE_L7DATA;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    i_ret = __req_parse(p_packet, p_st_private, p_st_body_data, p_st_conn);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SET_REQ(APM_L7_ANALYZE_SUCC));

    cassandra_session_destroy(p_st_private);
    frreport_body_free(&p_st_body_data);
    frfree(p_st_conn);
    if(p_packet)
    {
        bytebuff_free(&p_packet);
        p_packet = NULL;
    }
    return;

ERROR:
    if(p_st_private)
        cassandra_session_destroy(p_st_private);
    if(p_st_body_data)
        frreport_body_free(&p_st_body_data);
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_packet)
    {
        bytebuff_free(&p_packet);
        p_packet = NULL;
    }
    return;
}

static void case6(void)//response
{
    int i_ret = 0;
	char *p_data = NULL;
    Frbyte_Packet *p_packet = NULL;
    L7session_cassandra_private *p_st_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Conntrack *p_st_conn = NULL;

    p_data = (char *)"\x84\x00\x00\x01\x02\x00\x00\x00\x00";
    p_packet = bytebuff_creat();
    if(p_packet == NULL)
        goto ERROR;
    if(bytebuff_add(p_packet, p_data, 9) != FR_SUCC)
        goto ERROR;

    p_st_private = (L7session_cassandra_private *)frmalloc(sizeof(L7session_cassandra_private));
    if(p_st_private == NULL)
        goto ERROR;
    memset(p_st_private, 0x00, sizeof(L7session_cassandra_private));

    p_st_body_data = (Frreport_body_data *)frmalloc(sizeof(Frreport_body_data));
    if(p_st_body_data == NULL)
        goto ERROR;
    memset(p_st_body_data, 0x00, sizeof(Frreport_body_data));
    p_st_body_data->em_type = FRREPORT_TYPE_L7DATA;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    i_ret = __req_parse(p_packet, p_st_private, p_st_body_data, p_st_conn);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);

    cassandra_session_destroy(p_st_private);
    frreport_body_free(&p_st_body_data);
    frfree(p_st_conn);
    if(p_packet)
    {
        bytebuff_free(&p_packet);
        p_packet = NULL;
    }
    return;

ERROR:
    if(p_st_private)
        cassandra_session_destroy(p_st_private);
    if(p_st_body_data)
        frreport_body_free(&p_st_body_data);
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_packet)
    {
        bytebuff_free(&p_packet);
        p_packet = NULL;
    }
    return;
}

int add_suite__req_parse(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" __req_parse()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", case1) == NULL ||
        CU_add_test(pSuite, "case2", case2) == NULL ||
        CU_add_test(pSuite, "case3", case3) == NULL ||
        CU_add_test(pSuite, "case4", case4) == NULL ||
        CU_add_test(pSuite, "case5", case5) == NULL ||
        CU_add_test(pSuite, "case6", case6) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
